/*   1:    */ package com.atomikos.icatch.jta;
/*   2:    */ 
/*   3:    */ import com.atomikos.icatch.system.Configuration;
/*   4:    */ import java.io.Externalizable;
/*   5:    */ import java.io.IOException;
/*   6:    */ import java.io.ObjectInput;
/*   7:    */ import java.io.ObjectOutput;
/*   8:    */ import java.rmi.RemoteException;
/*   9:    */ import java.util.Hashtable;
/*  10:    */ import javax.naming.Context;
/*  11:    */ import javax.naming.InitialContext;
/*  12:    */ import javax.naming.NamingException;
/*  13:    */ import javax.naming.RefAddr;
/*  14:    */ import javax.naming.Reference;
/*  15:    */ import javax.naming.Referenceable;
/*  16:    */ import javax.naming.StringRefAddr;
/*  17:    */ import javax.rmi.PortableRemoteObject;
/*  18:    */ import javax.transaction.HeuristicMixedException;
/*  19:    */ import javax.transaction.HeuristicRollbackException;
/*  20:    */ import javax.transaction.NotSupportedException;
/*  21:    */ import javax.transaction.RollbackException;
/*  22:    */ import javax.transaction.SystemException;
/*  23:    */ import javax.transaction.Transaction;
/*  24:    */ import javax.transaction.TransactionManager;
/*  25:    */ import javax.transaction.UserTransaction;
/*  26:    */ 
/*  27:    */ public final class RemoteClientUserTransaction
/*  28:    */   implements UserTransaction, Externalizable, Referenceable
/*  29:    */ {
/*  30:    */   static final int DEFAULT_TIMEOUT = 30;
/*  31:    */   private transient UserTransactionServer txmgrServer_;
/*  32:    */   private transient TransactionManager txmgr_;
/*  33:    */   private transient Hashtable threadToTidMap_;
/*  34:    */   private int timeout_;
/*  35:    */   private String name_;
/*  36:    */   private String initialContextFactory_;
/*  37:    */   private String providerUrl_;
/*  38:    */   private boolean imported_;
/*  39:    */   
/*  40:    */   public RemoteClientUserTransaction()
/*  41:    */   {
/*  42:111 */     this.threadToTidMap_ = new Hashtable();
/*  43:112 */     this.timeout_ = 30;
/*  44:113 */     this.imported_ = false;
/*  45:    */   }
/*  46:    */   
/*  47:    */   public RemoteClientUserTransaction(String name, String initialContextFactory, String providerUrl)
/*  48:    */   {
/*  49:131 */     this.initialContextFactory_ = initialContextFactory;
/*  50:132 */     this.providerUrl_ = providerUrl;
/*  51:    */     
/*  52:134 */     this.name_ = name;
/*  53:135 */     this.threadToTidMap_ = new Hashtable();
/*  54:136 */     this.timeout_ = 30;
/*  55:137 */     this.imported_ = false;
/*  56:    */   }
/*  57:    */   
/*  58:    */   private String getNotFoundMessage()
/*  59:    */   {
/*  60:152 */     String errorMsg = "Name not found: " + this.name_ + "\n" + "Please check that: \n" + "\t-server property com.atomikos.icatch.client_demarcation is set to true \n" + "   -server property com.atomikos.icatch.rmi_export_class is correct \n" + "\t-server property java.naming.factory.initial is " + this.initialContextFactory_ + "\n" + "\t-server property java.naming.provider.url is " + this.providerUrl_ + "\n" + "\t-the naming service is running on port " + this.providerUrl_ + "\n" + "\t-the transaction server is running";
/*  61:    */     
/*  62:    */ 
/*  63:    */ 
/*  64:    */ 
/*  65:    */ 
/*  66:    */ 
/*  67:    */ 
/*  68:    */ 
/*  69:    */ 
/*  70:    */ 
/*  71:    */ 
/*  72:    */ 
/*  73:165 */     return errorMsg;
/*  74:    */   }
/*  75:    */   
/*  76:    */   private boolean checkSetup()
/*  77:    */   {
/*  78:178 */     this.txmgr_ = TransactionManagerImp.getTransactionManager();
/*  79:181 */     if (this.txmgr_ == null)
/*  80:    */     {
/*  81:    */       try
/*  82:    */       {
/*  83:184 */         Hashtable env = new Hashtable();
/*  84:185 */         env.put("java.naming.factory.initial", this.initialContextFactory_);
/*  85:    */         
/*  86:187 */         env.put("java.naming.provider.url", this.providerUrl_);
/*  87:188 */         Context ctx = new InitialContext(env);
/*  88:189 */         this.txmgrServer_ = ((UserTransactionServer)PortableRemoteObject.narrow(ctx.lookup(this.name_), UserTransactionServer.class));
/*  89:    */       }
/*  90:    */       catch (Exception e)
/*  91:    */       {
/*  92:194 */         e.printStackTrace();
/*  93:195 */         throw new RuntimeException(getNotFoundMessage());
/*  94:    */       }
/*  95:197 */       if (this.txmgrServer_ == null) {
/*  96:198 */         throw new RuntimeException(getNotFoundMessage());
/*  97:    */       }
/*  98:    */     }
/*  99:201 */     return this.txmgr_ != null;
/* 100:    */   }
/* 101:    */   
/* 102:    */   private synchronized void setThreadMapping(String tid)
/* 103:    */   {
/* 104:206 */     Thread thread = Thread.currentThread();
/* 105:207 */     this.threadToTidMap_.put(thread, tid);
/* 106:    */   }
/* 107:    */   
/* 108:    */   private synchronized String removeThreadMapping()
/* 109:    */   {
/* 110:212 */     Thread thread = Thread.currentThread();
/* 111:213 */     return (String)this.threadToTidMap_.remove(thread);
/* 112:    */   }
/* 113:    */   
/* 114:    */   private synchronized String getThreadMapping()
/* 115:    */   {
/* 116:218 */     Thread thread = Thread.currentThread();
/* 117:219 */     return (String)this.threadToTidMap_.get(thread);
/* 118:    */   }
/* 119:    */   
/* 120:    */   public void begin()
/* 121:    */     throws NotSupportedException, SystemException
/* 122:    */   {
/* 123:228 */     boolean local = checkSetup();
/* 124:229 */     if (local)
/* 125:    */     {
/* 126:230 */       this.txmgr_.begin();
/* 127:    */     }
/* 128:    */     else
/* 129:    */     {
/* 130:233 */       String tid = getThreadMapping();
/* 131:234 */       if (tid != null) {
/* 132:236 */         throw new NotSupportedException("Nested transaction not allowed here");
/* 133:    */       }
/* 134:    */       try
/* 135:    */       {
/* 136:241 */         tid = this.txmgrServer_.begin(this.timeout_);
/* 137:    */       }
/* 138:    */       catch (RemoteException re)
/* 139:    */       {
/* 140:243 */         throw new SystemException(re.getMessage());
/* 141:    */       }
/* 142:245 */       setThreadMapping(tid);
/* 143:    */     }
/* 144:    */   }
/* 145:    */   
/* 146:    */   public void commit()
/* 147:    */     throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException, IllegalStateException, SecurityException
/* 148:    */   {
/* 149:259 */     boolean local = checkSetup();
/* 150:260 */     if (local)
/* 151:    */     {
/* 152:261 */       this.txmgr_.commit();
/* 153:    */     }
/* 154:    */     else
/* 155:    */     {
/* 156:264 */       if (this.imported_) {
/* 157:265 */         throw new SecurityException("Commit not allowed: not creator");
/* 158:    */       }
/* 159:267 */       String tid = removeThreadMapping();
/* 160:268 */       if (tid == null) {
/* 161:269 */         throw new IllegalStateException("No transaction for thread");
/* 162:    */       }
/* 163:    */       try
/* 164:    */       {
/* 165:272 */         this.txmgrServer_.commit(tid);
/* 166:    */       }
/* 167:    */       catch (RemoteException re)
/* 168:    */       {
/* 169:274 */         throw new SystemException(re.getMessage());
/* 170:    */       }
/* 171:    */     }
/* 172:    */   }
/* 173:    */   
/* 174:    */   public void rollback()
/* 175:    */     throws IllegalStateException, SystemException, SecurityException
/* 176:    */   {
/* 177:287 */     boolean local = checkSetup();
/* 178:288 */     if (local)
/* 179:    */     {
/* 180:289 */       this.txmgr_.rollback();
/* 181:    */     }
/* 182:    */     else
/* 183:    */     {
/* 184:292 */       if (this.imported_) {
/* 185:293 */         throw new SecurityException("Rollback not allowed: not creator");
/* 186:    */       }
/* 187:295 */       String tid = removeThreadMapping();
/* 188:296 */       if (tid == null) {
/* 189:297 */         throw new IllegalStateException("No transaction for thread");
/* 190:    */       }
/* 191:    */       try
/* 192:    */       {
/* 193:300 */         this.txmgrServer_.rollback(tid);
/* 194:    */       }
/* 195:    */       catch (RemoteException re)
/* 196:    */       {
/* 197:302 */         throw new SystemException(re.getMessage());
/* 198:    */       }
/* 199:    */     }
/* 200:    */   }
/* 201:    */   
/* 202:    */   public void setRollbackOnly()
/* 203:    */     throws IllegalStateException, SystemException
/* 204:    */   {
/* 205:314 */     boolean local = checkSetup();
/* 206:315 */     if (local)
/* 207:    */     {
/* 208:316 */       this.txmgr_.setRollbackOnly();
/* 209:    */     }
/* 210:    */     else
/* 211:    */     {
/* 212:319 */       String tid = getThreadMapping();
/* 213:320 */       if (tid == null) {
/* 214:321 */         throw new IllegalStateException("No transaction for thread");
/* 215:    */       }
/* 216:    */       try
/* 217:    */       {
/* 218:323 */         this.txmgrServer_.setRollbackOnly(tid);
/* 219:    */       }
/* 220:    */       catch (RemoteException re)
/* 221:    */       {
/* 222:325 */         throw new SystemException(re.getMessage());
/* 223:    */       }
/* 224:    */     }
/* 225:    */   }
/* 226:    */   
/* 227:    */   public int getStatus()
/* 228:    */     throws SystemException
/* 229:    */   {
/* 230:336 */     int ret = 6;
/* 231:337 */     boolean local = checkSetup();
/* 232:338 */     if (local)
/* 233:    */     {
/* 234:339 */       ret = this.txmgr_.getStatus();
/* 235:    */     }
/* 236:    */     else
/* 237:    */     {
/* 238:342 */       String tid = getThreadMapping();
/* 239:343 */       if (tid != null) {
/* 240:    */         try
/* 241:    */         {
/* 242:345 */           ret = this.txmgrServer_.getStatus(tid);
/* 243:    */         }
/* 244:    */         catch (RemoteException re)
/* 245:    */         {
/* 246:347 */           throw new SystemException(re.getMessage());
/* 247:    */         }
/* 248:    */       }
/* 249:    */     }
/* 250:351 */     return ret;
/* 251:    */   }
/* 252:    */   
/* 253:    */   public void setTransactionTimeout(int seconds)
/* 254:    */     throws SystemException
/* 255:    */   {
/* 256:361 */     this.timeout_ = seconds;
/* 257:    */   }
/* 258:    */   
/* 259:    */   public String toString()
/* 260:    */   {
/* 261:374 */     String ret = null;
/* 262:375 */     boolean local = checkSetup();
/* 263:376 */     if (local)
/* 264:    */     {
/* 265:377 */       Transaction tx = null;
/* 266:    */       try
/* 267:    */       {
/* 268:379 */         tx = this.txmgr_.getTransaction();
/* 269:    */       }
/* 270:    */       catch (SystemException e)
/* 271:    */       {
/* 272:381 */         String msg = "Error getting transaction";
/* 273:382 */         Configuration.logWarning(msg, e);
/* 274:    */       }
/* 275:384 */       if (tx != null) {
/* 276:385 */         ret = tx.toString();
/* 277:    */       }
/* 278:    */     }
/* 279:391 */     if (ret == null) {
/* 280:392 */       ret = getThreadMapping();
/* 281:    */     }
/* 282:394 */     return ret;
/* 283:    */   }
/* 284:    */   
/* 285:    */   public Reference getReference()
/* 286:    */     throws NamingException
/* 287:    */   {
/* 288:409 */     RefAddr nameRef = new StringRefAddr("ServerName", this.name_);
/* 289:410 */     RefAddr urlRef = new StringRefAddr("ProviderUrl", this.providerUrl_);
/* 290:411 */     RefAddr factRef = new StringRefAddr("ContextFactory", this.initialContextFactory_);
/* 291:    */     
/* 292:413 */     RefAddr timeoutRef = new StringRefAddr("Timeout", new Integer(this.timeout_).toString());
/* 293:    */     
/* 294:415 */     Reference ref = new Reference(getClass().getName(), new StringRefAddr("name", "RemoteClientUserTransaction"), RemoteClientUserTransactionFactory.class.getName(), null);
/* 295:    */     
/* 296:    */ 
/* 297:418 */     ref.add(nameRef);
/* 298:419 */     ref.add(urlRef);
/* 299:420 */     ref.add(factRef);
/* 300:421 */     ref.add(timeoutRef);
/* 301:422 */     return ref;
/* 302:    */   }
/* 303:    */   
/* 304:    */   public void writeExternal(ObjectOutput out)
/* 305:    */     throws IOException
/* 306:    */   {
/* 307:443 */     String tid = getThreadMapping();
/* 308:444 */     out.writeObject(tid);
/* 309:445 */     out.writeObject(this.name_);
/* 310:446 */     out.writeObject(this.initialContextFactory_);
/* 311:447 */     out.writeObject(this.providerUrl_);
/* 312:448 */     out.writeInt(this.timeout_);
/* 313:    */   }
/* 314:    */   
/* 315:    */   public void readExternal(ObjectInput in)
/* 316:    */     throws IOException, ClassNotFoundException
/* 317:    */   {
/* 318:462 */     String tid = (String)in.readObject();
/* 319:463 */     if (tid != null)
/* 320:    */     {
/* 321:464 */       setThreadMapping(tid);
/* 322:465 */       this.imported_ = true;
/* 323:    */     }
/* 324:469 */     this.name_ = ((String)in.readObject());
/* 325:470 */     this.initialContextFactory_ = ((String)in.readObject());
/* 326:471 */     this.providerUrl_ = ((String)in.readObject());
/* 327:472 */     this.timeout_ = in.readInt();
/* 328:    */   }
/* 329:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions-jta\3.7.0\transactions-jta-3.7.0.jar
 * Qualified Name:     com.atomikos.icatch.jta.RemoteClientUserTransaction
 * JD-Core Version:    0.7.0.1
 */